<?php

include_once 'data.php';
include_once 'functions.php';

require_once('application/model/sql/Connection.class.php');
require_once('application/model/sql/ConnectionFactory.class.php');
require_once('application/model/sql/ConnectionProperty.class.php');
require_once('application/model/sql/QueryExecutor.class.php');
require_once('application/model/sql/Transaction.class.php');
require_once('application/model/sql/SqlQuery.class.php');
require_once('application/model/core/ArrayList.class.php');
require_once('application/model/dao/DAOFactory.class.php');
        
require_once 'application/controller/../model/dao/DAOFactory.class.php';
require_once 'application/controller/../model/dao/UsersDAO.class.php';
require_once 'application/controller/../model/impl/UsersSqliteDAO.class.php';
require_once 'application/controller/../model/impl/ext/UsersSqliteExtDAO.class.php';
require_once 'lib/LogConsole.php';
require_once 'application/model/dto/User.class.php';
require_once 'lib/LDAPHandler.php';


$username=$_POST['user'];
$password=$_POST['pass'];

$ldap = new LDAPHandler();
$user = $ldap->authenticate($username,$password);
if($user != null){
    
    echo "user ok";
    
    if(DAOFactory::getUsersDAO()->verify($user)){
        
        echo "verify ok";
        
        $_SESSION["user_id"] = 1; //$user->getUserId();                
        $_SESSION['user'] = $user->getUsername();
        $_SESSION['password'] = $user->getPassword();
        $_SESSION['permissions'] = "U";
        $_SESSION['auth'] = true;
            
        if(DAOFactory::getUsersDAO()->registerSession($temp_dir,$dbHandle)){            
            die("OK");
        }  else {
            die("shame");
        }
        
            ////echo "Login Success: ".$user->getUsername();        
    }else
        die("Password typo.");
}else{
    die("Bad username or password.");
}


?>